<?php
class App_Module_Admin_Component_Media_Ctrl_Http_Web extends App_Module_Admin_Component_Media_Ctrl_Http
{
    protected function PrimaryActionInit ($basicActionName)
    {
		Mozg::Layout('admin')->SetMenuActiveElement('media/list');
    }

    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
		$this->listPr();
    }

    public function listPrAction()
    {
    	$this->GetQueryPage();

        $filter = $this->GetFilter();

        $t = $this->GetTpl('List');
        Mozg::Response('http')->SetBody($t->GetList($this->GetName(), $filter, $this->_page));

        Mozg::SubAction()->Set('Left Sidebar', array($this, 'OutFilterInBlock'), array($filter));
    }

    public function addPrAction($file = null)
    {
        if (!$file)
        {
        	$file = $this->GetMediaFromQueryId();
	   		if (!$file) $file = Mozg_Mapper::Get('Media')->GetEntity();
        }

        $t = $this->GetTpl('Editform');
        Mozg::Response('http')->SetBody($t->GetForm($this->GetName(), $file));

        Mozg::Layout('admin')->SetMenuActiveElement('media/add');
    }

    public function savePrAction()
    {
       	$file = $this->GetMediaFromQueryId();
   		if (!$file) $file = Mozg_Mapper::Get('Media')->GetEntity();

   		$this->GetTpl('Editform')->AddPostData($file, (array)Mozg::Request('http')->getPost('media'));

		if (Mozg_Mapper::Get('Media')->Save($file))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_SAVED'));
	       	Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));
        }
        else
        {
			if (!$file->Validator()->GetState())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
        	}
        	else
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_NOT_SAVED'), MESSAGE_ERROR);
        	}

        	$this->addPr($file);
            return;
        }
    }

    public function delPrAction()
    {
       	$file = $this->GetMediaFromQueryId();
   		if (!$file)
   		{
   			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('FILE_NOT_FOUND'), MESSAGE_ERROR);
			Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));
   		}

   		if (Mozg_Mapper::Get('Media')->Delete(array('id' => $file->GetId())))
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('FILE_DELETED'));
   		}
   		else
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('UNABLE_TO_DELETE_FILE'), MESSAGE_ERROR);
   		}

		Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));
    }

    public function mediablock($baseUrlParams, $params = array())
    {
        $filterBlock = $this->GetCompactFilter('mediablock', $baseUrlParams);
        $filesBlock = $this->OutMediaFilesBlock($baseUrlParams, $params);

        return $this->GetTpl('Web')->MediaBlock($baseUrlParams, $filterBlock, $filesBlock);
    }

    private function GetCompactFilter($prefixName = 'media', $baseUrlParams)
    {
        $t = $this->GetTpl('Compactfilterform');
        return $t->GetForm($this->GetFilter($prefixName), $baseUrlParams);
    }

    public function OutFilterInBlock($filter)
    {
    	$baseUrlParams = array( 'module' => 'admin', 'ctrl' => $this->GetName(), 'action' => 'list');

        $t = $this->GetTpl('Filterform');
        return $t->GetForm($filter, $baseUrlParams);
    }
}